﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace Gmantis.Controls.Extended
{
    [TemplatePart(Name = "BottomTrack", Type = typeof(FrameworkElement)), TemplatePart(Name = "TopTrack", Type = typeof(FrameworkElement))]
    public class ColorSlider : Slider
    {
        // Fields
        internal FrameworkElement _elementBottomTrack;
        internal FrameworkElement _elementTopTrack;
        internal bool _isLoaded;
        internal const string BottomTrackElementName = "BottomTrack";
        internal const string TopTrackElementName = "TopTrack";

        // Methods
        public ColorSlider()
        {
            base.DefaultStyleKey = typeof(ColorSlider);
            SetCustomDefaultValues();
        }

        private T GetTemplateChild<T>(string childName, bool required, ref string errors) where T : class
        {
            DependencyObject templateChild = base.GetTemplateChild(childName);
            ApplyTemplateHelper.VerifyTemplateChild(typeof(T), "template part", childName, templateChild, required, ref errors);
            return (templateChild as T);
        }

        private static Storyboard GetTemplateChildResource(FrameworkElement root, string resourceName, bool required, ref string errors)
        {
            return GetTemplateChildResource<Storyboard>(root, resourceName, required, ref errors);
        }

        private static T GetTemplateChildResource<T>(FrameworkElement root, string resourceName, bool required, ref string errors) where T : class
        {
            object child = root.Resources[resourceName];
            ApplyTemplateHelper.VerifyTemplateChild(typeof(T), "resource", resourceName, child, required, ref errors);
            return (child as T);
        }

        private void InitializeBottomTrackPart()
        {
            this._elementBottomTrack.MouseLeftButtonDown += new MouseButtonEventHandler(this.MoveThumb);
        }

        private void InitializeTopTrackPart()
        {
            this._elementTopTrack.MouseLeftButtonDown += new MouseButtonEventHandler(this.MoveThumb);
        }

        private void MoveThumb(object sender, MouseButtonEventArgs e)
        {
            Point position = e.GetPosition(this);
            Thumb templateChild = (Thumb)base.GetTemplateChild("VerticalThumb");
            base.Value = base.Minimum + ((base.Maximum * ((base.ActualHeight - position.Y) - (templateChild.ActualHeight / 2.0))) / (base.ActualHeight - templateChild.ActualHeight));
        }

        public override void OnApplyTemplate()
        {
            string errors = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            this._elementTopTrack = this.GetTemplateChild<FrameworkElement>("TopTrack", false, ref errors);
            if (this._elementTopTrack != null)
            {
                this.InitializeTopTrackPart();
            }
            this._elementBottomTrack = this.GetTemplateChild<FrameworkElement>("BottomTrack", false, ref errors);
            if (this._elementBottomTrack != null)
            {
                this.InitializeBottomTrackPart();
            }
            if (!string.IsNullOrEmpty(errors))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to ColorSlider.\nDetails: {0}", new object[] { errors }));
                }
            }
        }

        private void SetCustomDefaultValues()
        {
        }
    }
}
